package cn.xiaobai.j2exe;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 场景管理器
 *
 * @Author yangdaji
 * @Date 2023/12/26
 * @PackageName cn.xiaobai.j2exe
 * @ClassName StageManager
 */
public class StageManager {

    /**
     * 场景映射, key: 触发id, value: 节点
     */
    private static Map<String, Node> triggerIdNodeMap = new ConcurrentHashMap<>();

    /**
     * 场景映射, key: 触发id, value: fxml
     */
    private static Map<String, FXMLLoader> triggerIdFxmlMap = new ConcurrentHashMap<>();


    /**
     * 缓存fxml
     *
     * @param key      触发id
     * @param children 节点
     */
    public static void putFxml(String key, FXMLLoader children) {
        if (StrUtil.isBlank(key) || ObjectUtil.isEmpty(children)) {
            throw new IllegalArgumentException(key + " is null  or  children is null");
        }

        triggerIdFxmlMap.put(key, children);
    }

    /**
     * 获取fxml
     *
     * @param key 触发id
     * @return
     */
    public static FXMLLoader getFxml(String key) {
        if (StrUtil.isBlank(key)) {
            throw new IllegalArgumentException(key + " is null");
        }
        return triggerIdFxmlMap.get(key);
    }


    /**
     * 缓存节点
     *
     * @param key      触发id
     * @param children 节点
     */
    public static void putNode(String key, Node children) {
        if (StrUtil.isBlank(key) || ObjectUtil.isEmpty(children)) {
            throw new IllegalArgumentException(key + " is null  or  children is null");
        }
        triggerIdNodeMap.put(key, children);
    }

    /**
     * 获取节点
     *
     * @param key 触发id
     * @return
     */
    public static Node getNode(String key) {
        if (StrUtil.isBlank(key)) {
            throw new IllegalArgumentException(key + " is null");
        }
        return triggerIdNodeMap.get(key);
    }


}
